/**
 * Created By zhangQ 2018/5/9
 * [mysql数据库访问类]
 */
import mysql from 'mysql'

function executeQuery (sqlStr, connParam) {
  return new Promise((resolve, reject) => {
    // 创建链接
    let conn = mysql.createConnection({
      host: connParam.host,
      user: connParam.user,
      password: connParam.password,
      database: connParam.database,
      port: connParam.port
    })
    conn.connect((err) => {
      if (err) {
      } else {
        conn.query(sqlStr, (err, result, fields) => {
          if (err) {
            console.log('[INSERT ERROR] - ', err.message)
            reject(new Error(err.message))
            return
          }
          resolve({ res: result, fields })
        })
        console.log('conn.end()')
        conn.end()
      }
    })
  })
}

export default { executeQuery }
